//
//  PopHeaderView.m
//  customerGuide
//
//  Created by 王鹏宇 on 2017/4/6.
//  Copyright © 2017年 王鹏宇. All rights reserved.
//

#import "PopHeaderView.h"
#import "WeatherCarouselView.h"
#import "TabItem.h"
#define Height  self.frame.size.height
#define Width   self.frame.size.width

@interface PopHeaderView()

@property (nonatomic, strong)UILabel * locationTitleLabel;
@property (nonatomic, strong)UIButton * mapBtn;
@property (nonatomic, strong)UIView * titleView;
@property (nonatomic, strong)UIView * mapView;
@property (nonatomic, strong)UILabel * attractionsGuideLabel;
@property (nonatomic, strong)WeatherCarouselView * weatherCarouseView;


@property (nonatomic, strong) NSDictionary * items;

//@property (nonatomic, copy)NSString * CityName;
//@property (nonatomic, strong) NSArray *weatherArray;


@end
@implementation PopHeaderView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self createMennuView];
        [self createLocaltionTitleLabel];
        [self createAttractionsGuideView];
        [self createMapBtn];
        self.backgroundColor = [UIColor clearColor];
        [self updateLocation:frame];
        
    }
    return self;
}

- (void)setCity:(City *)city {
    
    _city = city;
    
    self.locationTitleLabel.text = city.smartName;
    self.weatherCarouseView.weatherArray = city.weather;
    
    if (city.hasstrategy) {
        [self.titleView addSubview:self.RaidersBtn];
    }else {
        [self.RaidersBtn removeFromSuperview];
    }
    [self setTabArray:city.tagsArray];
    
}


//- (void)setCityName:(NSString *)CityName {
//    _CityName = CityName;
//    self.locationTitleLabel.text = CityName;
//}

//- (void)setWeather:(NSString *)weather {
//    _weather = weather;
//    _weatherLabel.text = weather;
//}
//- (void)setWeatherIcon:(UIImage *)weatherIcon {
//    _weatherIcon = weatherIcon;
//    _weatherImageView.image = weatherIcon;
//}
- (void)createMapBtn {
    _mapView = [[UIView alloc] init];
    _mapBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_mapView];
    
}
- (void)createAttractionsGuideView {
    self.attractionsGuideImageView = [[UIImageView alloc] init];
    self.attractionsGuideImageView.hidden = YES;
    self.attractionsGuideImageView.image = [UIImage imageNamed:@"圆角矩形"];
    [self addSubview:self.attractionsGuideImageView];
    [self.attractionsGuideImageView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.mas_leading).offset(20);
        make.trailing.equalTo(self.mas_trailing).offset(-20);
        make.height.mas_equalTo(50);
        make.bottom.mas_equalTo(self.mas_bottom);
        
    }];
    
    self.attractionsGuideLabel = [[UILabel alloc] init];
    self.attractionsGuideLabel.text = MYLocalizedString(@"HOMEPAGE_HOTLIST", nil);
    self.attractionsGuideLabel.font = [UIFont systemFontOfSize:17 weight:10];
    self.attractionsGuideLabel.textAlignment = NSTextAlignmentCenter;
    self.attractionsGuideLabel.textColor = [UIColor whiteColor];
    
    [self.attractionsGuideImageView addSubview:self.attractionsGuideLabel];
    
    [self.attractionsGuideLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.attractionsGuideImageView.mas_leading).offset(20);
        make.trailing.equalTo(self.attractionsGuideImageView.mas_trailing).offset(-20);
        make.top.equalTo(self.attractionsGuideImageView.mas_top).offset(10);
        make.bottom.equalTo(self.attractionsGuideImageView.mas_bottom).offset(-10);
    }];
    
}

- (TabItem *)RaidersBtn {
    
    if (!_RaidersBtn) {
        _RaidersBtn = [TabItem initTabItemWith:@{@"title":@"HOMEPAGE_STRATEGY",@"imageName":@"推荐行程"}];
        _RaidersBtn.Ratio = 0.9;
        _RaidersBtn.frame = CGRectMake(SCREEN_WIDTH - 74, 50, 54, 60);
        
        _RaidersBtn.selectNum = RaidersItem;
        [_RaidersBtn addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _RaidersBtn;
}



- (void)createLocaltionTitleLabel {
    
    self.titleView = [[UIView alloc] init];
    
    [self addSubview:self.titleView];
    
    self.locationTitleLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, Width - 40, 40)];
    //self.locationTitleLabel.textAlignment = NSTextAlignmentCenter;
    self.locationTitleLabel.font = [UIFont systemFontOfSize:30 weight:8];
    self.locationTitleLabel.textColor = [UIColor whiteColor];
    self.locationTitleLabel.minimumScaleFactor = 0.5;
    self.locationTitleLabel.adjustsFontSizeToFitWidth = YES;
    [self.titleView addSubview:self.locationTitleLabel];
    
    _weatherCarouseView = [[WeatherCarouselView alloc] initWithFrame:CGRectMake(20, 50,120, 60)];
    [self.titleView addSubview:_weatherCarouseView];
    
    
}

//- (void)setWeatherArray:(NSArray *)weatherArray {
//    
//    _weatherArray = weatherArray;
//    
//    if (weatherArray.count > 0) {
//        self.weatherCarouseView.weatherArray = weatherArray;
//    }
//}


- (void)createMennuView {
    _mennuView = [[UIView alloc] init];
    
    [self addSubview:_mennuView];
}

- (void)selectedItem:(TabItem *) btn {
    
    if (_delegate && [self.delegate respondsToSelector:@selector(selecteItemWith:)]) {
        [self.delegate selecteItemWith:btn.selectNum];
    }
}


- (void)updateLocation:(CGRect)frame{
    self.frame = frame;
    self.titleView.frame = CGRectMake(0, (Height - 350)/4 > 80 ?80:(Height - 350)/4, Width, 110);
    
    CGFloat mapH = CGRectGetMaxY(self.titleView.frame);
    
    _mapView.frame = CGRectMake((SCREEN_WIDTH - 110)/2, mapH + (Height- 360)/4 , 110, 130);
    
    CGFloat menuH = CGRectGetMaxY(_mapView.frame);
    _mennuView.frame = CGRectMake(0,menuH + (Height- 360)/4 , SCREEN_WIDTH, 80);
    
}

- (NSDictionary *)items {
    
    if (!_items) {
        _items = @{@"view":@{@"title":@"HOMEPAGE_SITE",@"imageName":@"sightseeing",@"selectedNum":@(ScenicItem)},
                   @"line":@{@"title":@"HOMEPAGE_CITYTOUR",@"imageName":@"urbanline",@"selectedNum":@(CityTourItem)},
                   @"guide":@{@"title":@"HOMEPAGE_GUIDER",@"imageName":@"tourguide",@"selectedNum":@(GuiderItem)},
                   @"action":@{@"title":@"HOMEPAGE_EVENT",@"imageName":@"activities",@"selectedNum":@(ActionItem)}};
    }
    return _items;
}

- (void)setTabArray:(NSArray *)tabArray {
    
    [self resetMapView];
    
    if (_mennuView.subviews.count > 0) {
        for (UIView * view in _mennuView.subviews) {
            
            [view removeFromSuperview];
        }
    }
    CGFloat space = (SCREEN_WIDTH - (tabArray.count * 64))/ (tabArray.count + 1);
    
        for (int i= 0; i < tabArray.count; i++) {
            NSDictionary *dict = self.items[tabArray[i]];
            
            TabItem * item = [TabItem initTabItemWith:dict];
            CGFloat X = space + i * (60 + space);
            item.frame = CGRectMake(X, 0,64, 80);
            item.selectNum = [dict[@"selectedNum"] integerValue];
            [item addTarget:self action:@selector(selectedItem:) forControlEvents:UIControlEventTouchUpInside];
            
            if ([self.city.hots containsString:tabArray[i]]) {
                item.isHot = YES;
            }
            [_mennuView addSubview:item];
        }
    
}


- (void)resetMapView {
    
    if (_mapView.subviews.count > 0) {
        for (UIView * view in _mapView.subviews) {
            [view removeFromSuperview];
        }
    }
    _mapBtn.frame = CGRectMake(10, 0, 90, 90);
    [_mapView addSubview:_mapBtn];
    [_mapBtn setImage:[UIImage imageNamed:@"mapIcon"] forState:UIControlStateNormal];
    [_mapBtn addTarget:self action:@selector(Pop_mapBtnClicked) forControlEvents:UIControlEventTouchUpInside];
    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(10,100, 90, 20)];
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:16 weight:10];
    label.text = MYLocalizedString(@"HOMEPAGE_MAP", nil);
    label.textColor = [UIColor whiteColor];
    
    [_mapView addSubview:label];
}
#pragma mark 自己的代理方法

- (void)Pop_mapBtnClicked {
    if (_delegate && [_delegate respondsToSelector:@selector(mapBtnClicked)]) {
        [self.delegate mapBtnClicked];
    }
    
}

- (void)changeAllViewAlpha:(CGFloat)alpha {
    self.alpha = alpha;
    //    self.locationTitleLabel.alpha = alpha;
    //    self.mapBtn.alpha = alpha;
    //    self.mennuView.alpha = alpha;
}
@end
